#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e4 + 10, M = 1e5 + 10;
int a1[M], b1[M];
int h[N], e[M], ne[M], w[N], idx;
int dfn[N], low[N], tot;
int stk[N], instk[N], top;
int sz[N], scc[N], val[N];
int scc_cnt;
int din[N], dout[N], d[N];
int f[N];
int n, m;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void tarjan(int u)
{
	dfn[u] = low[u] = ++tot;
	stk[++top] = u, instk[u] = 1;
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (!dfn[j]) {
			tarjan(j);
			low[u] = min(low[u], low[j]);
		}
		else if (instk[j]) {
			low[u] = min(low[u], dfn[j]);
		}
	}
	if (dfn[u] == low[u]) {
		int y;
		scc_cnt++;
		do {
			y = stk[top--];
			instk[y] = 0;
			sz[scc_cnt] ++;
			scc[y] = scc_cnt;
			val[scc_cnt] += w[y];
		} while (y != u);
	}
}

void tuopu()
{
	queue<int>q;
	for (int i = 1; i <= scc_cnt; i++) {
		if (!d[i]) {
			q.push(i);
			f[i] = val[i];
		}
	}
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			f[j] = max(f[j], f[t] + val[j]);
			if (--d[j] == 0) {
				q.push(j);
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++)cin >> w[i];
	for (int i = 1; i <= m; i++) {
		int a, b;
		cin >> a >> b;
		a1[i] = a, b1[i] = b;
		add(a, b);
	}
	for (int i = 1; i <= n; i++) {
		if (!dfn[i]) {
			tarjan(i);
		}
	}
	//cout << scc_cnt << endl;
	//cout << val[1] << endl;
	/*for (int i = 1; i <= n; i++) {
		for (int j = h[i]; ~j; j = ne[j]) {
			int k = e[j];
			int a = scc[i], b = scc[k];
			if (a != b) {
				dout[a]++;
				din[b]++;
			}
		}
	}*/
	memset(h, -1, sizeof(h));
	memset(e, 0, sizeof(e));
	memset(ne, 0, sizeof(ne));
	idx = 0;
	for (int i = 1; i <= m; i++) {
		if (scc[a1[i]] != scc[b1[i]]) {
			add(scc[a1[i]], scc[b1[i]]);
			d[scc[b1[i]]]++;
		}
	}
	tuopu();
	int ans = 0;
	for (int i = 1; i <= scc_cnt; i++) {
		ans = max(ans, f[i]);
	}
	cout << ans << endl;
	return 0;
}